﻿using System;
using System.Linq;
using System.Data.Linq;
using System.Collections.Generic;
using System.Text;
using HR.Appointments.DataLayer;

namespace HR.Appointments.Service.DataAccessLayer.DLinq
{
    internal class CompanyDAO : ICompanyDAO
    {
        #region ICompanyDAO Members
        public CompanyEntity GetInstance(int companyId)
        {
            using (DAODataContext ctx = new DAODataContext())
            {
                var sql = from data in ctx.Companies
                          where data.CompanyId == companyId
                          && !data.EndDate.HasValue
                          select data;

                return CompanyEntity.Load(sql.FirstOrDefault());
            }
        }

        public List<CompanyEntity> GetAll()
        {
            var result = new List<CompanyEntity>();
            
            using (DAODataContext ctx = new DAODataContext())
            {
                var sql = from data in ctx.Companies
                          where !data.EndDate.HasValue
                          select data;

                foreach (HR.Appointments.DataLayer.Company row in sql)
                {   
                    result.Add(CompanyEntity.Load(row));
                }
            }
            return result;
        }

        public List<CompanyEntity> GetByName(string name)
        {
            var result = new List<CompanyEntity>();

            using (DAODataContext ctx = new DAODataContext())
            {
                var sql = from data in ctx.Companies
                          where !data.EndDate.HasValue
                          && (data.Name.StartsWith(name)
                          || data.Name.EndsWith(name))
                          && !data.EndDate.HasValue
                          select data;

                foreach (HR.Appointments.DataLayer.Company row in sql)
                {
                    result.Add(CompanyEntity.Load(row));
                }
            }
            return result;
        }

        public CompanyEntity GetCompanyByUser(string username, string password)
        {
            using (DAODataContext ctx = new DAODataContext())
            {
                var sql = from data in ctx.Companies
                          where data.Username == username
                          && data.Password == password
                          && !data.EndDate.HasValue
                          select data;

                if (sql.Count() != 0)
                {
                    return CompanyEntity.Load(sql.FirstOrDefault());
                }
                return CompanyEntity.Create("", "", "", "", "", "", "", "", 0);
            }
        }


        public void Insert(CompanyEntity company)
        {
            using (DAODataContext ctx = new DAODataContext())
            {
                ctx.Companies.InsertOnSubmit(company.ToData(null));
                ctx.SubmitChanges();
            }
        }

        public void GetByNationalId()
        {
            throw new System.NotImplementedException();
        }

		public void	Delete(int companyId)
		{
			using (DAODataContext ctx = new DAODataContext())
			{
				var sql = from data in ctx.Companies
						  where data.CompanyId == companyId
						  select data;

                Company cmp = sql.FirstOrDefault();
                if(!cmp.EndDate.HasValue)
                {
                    cmp.EndDate = DateTime.Now;
                }
                ctx.SubmitChanges();
			}
		}

		public void Update (CompanyEntity company)
		{
			using (DAODataContext ctx = new DAODataContext())
			{
				var sql = from data in ctx.Companies
						  where data.CompanyId == company.CompanyId
						  select data;

				Company row = company.ToData(sql.FirstOrDefault());
				ctx.SubmitChanges();
			}
        }
        #endregion
    }
}
